package com.wsz.oss.controller;

import com.wsz.oss.pojo.Photo;
import com.wsz.oss.pojo.Rtu;
import com.wsz.oss.server.OssService;
import com.wsz.pojo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class OssController {
    @Autowired
    private OssService ossService;

    @PostMapping("/fileoss")
    public R uploadOssFile(@RequestParam MultipartFile file) {
        String url = ossService.uploadFileAvatar(file);
        return R.ok().data("url",url);
    }

    @PostMapping("/weditor/fileoss")
    public Rtu WeditorUploadOssFile(@RequestParam MultipartFile file) {

        String url = ossService.uploadFileAvatar(file);
        String name = file.getOriginalFilename();

        Photo photo = new Photo();
        photo.setAlt(name);
        photo.setHref(url);
        photo.setUrl(url);
        Rtu rtu = new Rtu();
        rtu.setErrno(0);
        List<Photo> photoList = new ArrayList<>();

        photoList.add(photo);
        rtu.setData(photoList);
        rtu.setState(200);
        return rtu;
    }
}
